package com.study02.zuichangzifuchuan;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wwl on 2022/2/26 21:16
 */
public class Test {
    public int lengthOfLongestSubstring(String s) {
        int max = 0; //存储长度
        int len = s.length();
        Map<Character,Integer> map = new HashMap<>();
        for (int start = 0,end = 0; end < len; end++) {
            char element = s.charAt(end); //获取窗口的左侧字符
            if(map.containsKey(element)){ //判断map里是否包含该字符
                start = Math.max(map.get(element)+1,start);//如果包含窗口向后移map.get(element)+1该字符的位置的后一位
            }
            max=Math.max(max,end-start +1);
            map.put(element,end);//添加到map中
        }
        return max;
    }
}
